<template>
	<om-page title="登录">
		<view class="padding-xl text-center text-xxl bg-steelblue">设置新密码</view>
		<view class="content">
			<!-- #ifdef H5 || APP-PLUS -->
			<om-wing-blank>
				<view class="input-group">
					<view class="input-row border account-text-color" >
						<text class="title">用户名</text>
						<m-input v-model="userName" :isDisabled="true"></m-input>
					</view>
					<view class="input-row border">
						<text class="title">原密码</text>
						<m-input type="password" displayable v-model="pwd" placeholder="输入原密码"></m-input>
					</view>
					<view class="input-row border">
						<text class="title">新密码</text>
						<m-input type="password" displayable v-model="npwd" placeholder="输入新密码"></m-input>
					</view>
					<view class="input-row">
						<text class="title">确认密码</text>
						<m-input type="password" displayable v-model="repwd" placeholder="再次填写确认"></m-input>
					</view>
				</view>
				<om-white-space></om-white-space>
				<om-wing-blank class="tui-flex">
					<text class='text-sm bg-grey light'>{{rule}}</text>
				</om-wing-blank>
				<view class="btn-row" v-if="showBtn"><button type="primary" class="primary" @tap="bindConfirm">确认</button></view>
				<om-wing-blank class="tui-flex justify-end">
					<view @click="forgetPwd" class='cu-tag radius bg-grey'>忘记密码?</view>
				</om-wing-blank>
			</om-wing-blank>
			<!-- #endif -->
		</view>
	</om-page>
</template>

<script>
import md5 from '@/static/js/md5.js';
import {ignoreHtml, errorModal,confirmModal } from 'static/js/common.js'
import { mapState, mapActions } from 'pinia';
import {
		useLoginUserStore
	} from '@/stores/login-user.js';

const app = getApp();
export default {
	data() {
		return {
			pwd:"",
			npwd:'',
			repwd:"",
			account:'',
			rule:'密码必须是8-20位英文字母(不区分大小写)、数字、字符组合(不能是纯数字)',
			showBtn:true
		};
	},
	computed:{
		...mapState(useLoginUserStore, ['hasLogin', 'forcedLogin', 'userName','userId',"avatar","sexCode"
			// #ifdef MP-WEIXIN
			,'db','openid','orgCode'
			// #endif
		])
	},
	methods: {
		...mapActions(useLoginUserStore, ['logout'
		// #ifdef MP-WEIXIN
		,'setOrgCode','setSubUr','setUserId'
		// #endif
		]),
		forgetPwd(){
			uni.reLaunch({
				url:"../login/forgetpwd/forgetpwd"
			})
		},
		bindConfirm(){
			if(!this.pwd){
				this.om.toast("请输入原密码")
				return
			}
			if(!this.npwd){
				this.om.toast("请输入新密码")
				return
			}else{
				let isnum = /^\d+$/.test(this.npwd)
				if(isnum){
					this.om.toast(this.rule)
					return
				}
				
			}
			if(!this.repwd){
				this.om.toast("请确认密码")
				return
			}
			
			let _that = this
			this.om.request("temployee/updateNewPwd",{
				oldPwd:md5.hex_md5(this.pwd.toLowerCase()),
				newPwd:md5.hex_md5(this.npwd.toLowerCase()),
				rePwd:md5.hex_md5(this.repwd.toLowerCase())
			},
			'POST'
			).then(res=>{
				if(res.succeed){
					_that.showBtn = false
					this.om.toast('密码修改成功')
					setTimeout(()=>{
						// #ifdef H5 || APP-PLUS
							let _that = this;
							_that.om.request('loginCenter/h5Logout').then(res => {
								_that.account = _that.userName
								if (res.succeed) {
									_that.logout();
									_that.om.clearToken();
									uni.removeStorageSync("ORG_SITE");
									if (_that.forcedLogin) {
										uni.reLaunch({
											url: '../login/login/login?account='+_that.account
										});
									}
								}
							});
						// #endif
					},1000)
				}else{
					_that.om.toast(ignoreHtml(res.msg))
				}
			})
			
		}
	}
};
</script>

<style scoped>
	.account-text-color{
		color:#808080;
	},
	.forgetPwd{
		text-align: right;
		font-size: 14px;
		padding-top: 15px;
	}
	.forgetPwd a{
		text-decoration: none;
		color: #0faeff;
	}
</style>
